package com.xclink.sys.controller;

import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xclink.common.controller.BaseController;
import com.xclink.common.controller.ExecuteResult;
import com.xclink.common.utils.MD5Tools;
import com.xclink.sys.entity.User;
import com.xclink.sys.service.UserService;

@Controller
@RequestMapping("user")
public class UserController extends BaseController<User> {
	@Autowired
	private UserService userService;
	@RequestMapping
	public String index() {
		return "/business/user/index";
	}

	// 查询所有普通用户
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(User user, HttpSession session, Model model, @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		User user1 = (User) session.getAttribute("currentUser");
	String type =	user1.getUserType();
	if("0".equals(type)){
		
	}else{
		if(user.getUserType()==null){
			user.setUserType("2");
		}
	}
		model.addAttribute("pageInfo", userService.findPageInfo(user, pageNum, pageSize));
		model.addAttribute("user", user);
		return "/business/user/userlist";
	}

	// 创建用户页面
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String createForm() {
		return "/business/user/create";
	}
	
	// 跳转到注册页面
		@RequestMapping(value = "/register", method = RequestMethod.GET)
		public String login() {
			return "/business/user/register";
		}

	// 保存注册用户信息
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(User user, Model model) {
		// 根据用户名验证账号是否存在
		if (userService.findUserByAccount(user.getUsername()) != null) {
			model.addAttribute("message", "用户已存在");
			return ExecuteResult.jsonReturn(300,"该账号已存在",true);
		}
		try {
			// 保存注册信息
			// 随机生成一个盐
			String salt = UUID.randomUUID().toString().replaceAll("-", "");
			String password = user.getPassword();
			password = MD5Tools.MD5(password + salt);
			user.setPassword(password);
			user.setSalt(salt);
			userService.save(user);
			return ExecuteResult.jsonReturn(200);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300);
		}
	}

	// 跳转到修改页面
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String editForm(Model model, @PathVariable("id") Long id) {
		// 根据主键查询信息
		model.addAttribute("user", userService.get(id));
		return "/business/user/edit";
	}

	// 修改数据
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(User user){
		try {
			userService.updateNotNull(user);
			return ExecuteResult.jsonReturn(200);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300);
		}

	}

	// 删除
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request) {
		String dels = request.getParameter("dels");
		try {
			String[] array = dels.split(",");
//			for (String i : array) {
//				Long id = Long.valueOf(i);
//				userService.deleteByPrimaryKey(id);
//			}
			userService.deleteAll(array);
			return ExecuteResult.jsonReturn(200);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300);
		}

	}

	// 修改密码
	@RequestMapping(value = "/editPassword", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> checkpassword( String username, String oldPassword, String newPassword) {
		try {
			boolean a = userService.updatePassword(username, oldPassword, newPassword);
			if(!a){
				return ExecuteResult.jsonReturn(300,"密码输入错误",false);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300,"密码修改失败",false);
		}
		return ExecuteResult.jsonReturn(200,"密码修改成功",true);
	}

	
	
	
	// 跳转到个人资料页面
	@RequestMapping(value = "/toInfo", method = RequestMethod.GET)
	public String information(HttpSession session, Model model) {
		User user = (User) session.getAttribute("currentUser");
		user =userService.get(user.getUserId());
		model.addAttribute("currentUser", user);
		session.setAttribute("currentUser", user);
		return "/business/user/information";
	}

	// 修改个人资料
	@RequestMapping(value = "/editInfo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editInfo(User user) {
		try {
			userService.updateNotNull(user);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300,"修改失败",false);
		}
		return ExecuteResult.jsonReturn(200,"成功",true);

	}

}
